package com.module.check.manager;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import base.common.CUDEnum;
import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;
import base.util.Page;

import com.module.check.dao.PersonalTaskDao;
import com.module.check.util.ReturnUtil;
import com.module.check.vo.CheckTask;
import com.module.check.vo.PersonalTask;

import flex.messaging.io.ArrayList;

public class PersonalTaskManager extends GenericHibernateManager<PersonalTask, Long> {
	
	private PersonalTaskManager(){}
	
	private static PersonalTaskManager manager;
	
	public synchronized static PersonalTaskManager getInstance(){
		if(manager == null)
			manager = new PersonalTaskManager();
		return manager;
	}
	
	private PersonalTaskDao dao = PersonalTaskDao.getInstance();
	
	/**
	 * 查询稽查任务列表
	 * @param pageObj
	 * @param pu
	 * @return
	 * @throws Exception
	 */
	public Page list(Page pageObj,PersonalTask pu) throws Exception{
		Session session = this.getSession();
		Page page = new Page();
		try {
			page = dao.getList(session,pu, pageObj.getStart(),pageObj.getLimit());
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return page;
	}
	/**
	 * 新增/修改
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	 public Map<String,Object> add(PersonalTask obj) throws Exception
	    {
	    	Session session = this.getSession();
	    	Transaction tx =session.beginTransaction();
	    	boolean flag = false;
	    	try{
	    		 if(null!=obj.getId()){//修改
	                 obj.setStatus("Y");
	                 obj.setExecutTime(new Date());
	                 flag = dao.update(session, obj);

	             }else{//新增
	                 obj.setStatus("N");	
//	                 obj.setExecutTime(new Date());
	                 flag = dao.save(session, obj);
	             }
	    		tx.commit();
	    	}catch(Exception e){
	    		tx.rollback();
	    		throw e;	    		
	    	}finally{
	    		this.closeSession();
	    	}
	    	Map<String,Object> map = new HashMap<String,Object>();
	    	map = ReturnUtil.optHandle(flag);
	    	return map;
	    }
	 /**
	  * 任务分配
	  * @param tasklist
	  * @param taskId
	  * @return
	  * @throws Exception
	  */
	 public Map<String,Object> add(List<PersonalTask> tasklist,Long taskId) throws Exception{
		 Session session = this.getSession();
			String delhql = "delete from PersonalTask as obj where obj.taskId="+taskId;
			boolean isTrue =false;
			Transaction tx = null;
			tx = session.beginTransaction();
			try {
				dao.deleteByQuery(session,delhql);
				for (PersonalTask personalTask : tasklist) {
					personalTask.setStatus("N");
//					personalTask.setExecutTime(new Date());
					isTrue = dao.save(session, personalTask);
					
				}
				tx.commit();
			} catch (Exception e) {
				tx.rollback();
				throw e;
			} finally {
				this.closeSession();
			}
		    	Map<String,Object> map = new HashMap<String,Object>();
		    	map = ReturnUtil.optHandle(isTrue);
		    	return map;
	    }
	/**
	 * 删除任务
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	  public Map<String,Object> delete(Long id) throws Exception
	    {
	    	Session session = this.getSession();
	    	Transaction tx =session.beginTransaction();
	    	boolean flag = false;
	    	try{
	    		PersonalTask obj = dao.get(session, id);
	    		obj.setStatus(CUDEnum.D.toString());
	    		flag = dao.update(session, obj);
	            tx.commit();
	    	}catch(Exception e){
	            tx.rollback();
	            throw e;
	            
	    	}finally{
	    		this.closeSession();
	    	}
	    	Map<String,Object> map = new HashMap<String,Object>();
	    	map = ReturnUtil.optHandle(flag);
	    	return map;

	    }
	  /**
	   * 根据id查找
	   * @param id
	   * @return
	   * @throws Exception
	   */
	   public PersonalTask findById(Long id) throws Exception{
		   Session session = this.getSession();
		   PersonalTask vo = new PersonalTask();
		   try{
			   vo = dao.get(session, id);
			
			   Query task = session.createQuery("from CheckTask where id = "+vo.getTaskId());
			   List<CheckTask> tasklist = task.list();
			   String taskDetail = "";
				if(tasklist.size()>0)
					taskDetail = tasklist.get(0).getDetail();
				
				vo.setDetail(taskDetail);
		   }catch (Exception e) {
				throw e;
		   }finally{
			   this.closeSession();
		   }
		   return vo;
	   }
	   /**
	    * 根据taskId查找
	    * @param id
	    * @return
	    * @throws Exception
	    */
	   public  List<PersonalTask> findByTaskId(Long id) throws Exception{
		   Session session = this.getSession();
		   List<PersonalTask> tasklist = new ArrayList();
		   try{			   
			   Query task = session.createQuery("from PersonalTask where taskId ="+id);
			   tasklist = task.list();
			   			  
		   }catch (Exception e) {
			   throw e;
		   }finally{
			   this.closeSession();
		   }
		   return tasklist;
	   }
	@Override
	public GenericHibernateDao<PersonalTask, Long> getDao() {
		// TODO Auto-generated method stub
		return null;
	}
}
